<!--
 * @Descripttion: 
 * @Version: 1.0
 * @Author: wcl
 * @Date: 2023-11-23 15:06:25
 * @LastEditors: wcl
 * @LastEditTime: 2023-11-23 15:15:44
-->
<template>
  <div>
    <a-modal v-model:open="modalOpen" :title="title" width="50%" :footer="null">
      <a-space direction="vertical" style="width: 100%" :size="16">
        <a-form
          :model="formData"
          ref="formRef"
          :rules="rules"
          labelAlign="left"
          :label-col="{ span: 3 }"
          :wrapper-col="{ span: 16 }"
        >
          <a-form-item label="类目名称" name="name">
            <a-input
              v-model:value="formData.name"
              maxlength="8"
              placeholder="输入类目名称"
            />
          </a-form-item>
          <a-form-item label="展示排序" name="sort">
            <a-input
              v-model:value="formData.sort"
              placeholder="输入排序数字编号"
            />
          </a-form-item>
          <a-form-item
            label="类目图标"
            name="coverPic"
            ref="uploadImg"
            :rules="[{ required: true, message: '请上传类目图标' }]"
          >
            <div class="img-item">
              <a-upload
                name="avatar"
                accept=".jpg,.png"
                list-type="picture-card"
                class="avatar-uploader"
                :show-upload-list="false"
                :action="null"
                :before-upload="beforeUpload"
                @change="handleChange"
              >
                <img
                  style="width: 102px; height: 102px"
                  v-if="formData.coverPic"
                  :src="formData.coverPic"
                  alt="avatar"
                />
                <div v-else>
                  <loading-outlined v-if="loading"></loading-outlined>
                  <plus-outlined v-else></plus-outlined>
                </div>
              </a-upload>
              <div class="img-label">仅支持上传.jpg.png格式</div>
            </div>
          </a-form-item>
        </a-form>
        <div class="modal-but-layout">
          <a-space wrap>
            <a-popconfirm
              title="您是否要数据重置?"
              ok-text="是"
              cancel-text="否"
              @confirm="confirm"
            >
              <a-button>重置</a-button>
            </a-popconfirm>
            <a-button type="primary" @click="handleSubmit">提交</a-button>
          </a-space>
        </div>
      </a-space>
    </a-modal>
  </div>
</template>
<script setup>
import { nextTick, ref } from "vue";
import { catalogSave } from "@/api/goodConfig.js";
import { singleUploadsFile } from "@/api/upload.js";
import { message } from "ant-design-vue";
const modalOpen = ref(false);
const formRef = ref(null);
const formData = ref({
  name: undefined,
  sort: undefined,
});
const rules = {
  name: [{ required: true, message: "请输入类目名称" }],
  sort: [{ required: true, message: "请输入序号" }],
  coverPic:[{ required: true, message: "请上传图标" }]
};
const title = ref("");
const open = (titles, record) => {
  console.log(record)
  title.value = titles;
  modalOpen.value = true;
  nextTick(() => {
    formData.value = Object.assign({}, record);
    formRef.value.resetFields();
  });
};
const loading = ref(false);
function beforeUpload(file) {
  //判断是否小于20M
  let isLt20M = file.size < 1024 * 1024 * 20;
  if (!isLt20M) {
    //将fileList中本次file文件删掉
    fileList.splice(fileList.indexOf(file), 1);
    message.error("所选文件单个超过20M，已自动过滤，请检查~");
  }
  return false;
}
const uploadImg = ref(null);
const handleChange = (file) => {
  loading.value = true;
  let formState = new FormData();
  formState.append("file", file.file);
  formState.append("type", "1");
  singleUploadsFile(formState).then((res) => {
    if (res.code == 200) {
      loading.value = false;
      formData.value.coverPic = res.data;
      uploadImg.value.clearValidate();
    }
  });
};
const emits = defineEmits(["refreshList"]);
const handleSubmit = async (res) => {
  formRef.value.validate().then((res) => {
    delete formData.value.state
    catalogSave(formData.value).then((result) => {
      if (result.code == 200) {
        message.success("保存成功");
        modalOpen.value = false;
        emits("refreshList");
      }
    });
  });
};
const confirm = () => {
  formData.value = {
    name: undefined,
    sort: undefined,
  };
};
defineExpose({ modalOpen, open });
</script>
<style lang="scss" scoped></style>
